//
//  OPCountryController.m
//  OnePiece
//
//  Created by lanou on 15/10/29.
//  Copyright © 2015年 wsy. All rights reserved.
//
#define FIRST @"http://api.breadtrip.com/destination/place/1/"
#define LAST @"/pois/all/?sort=default&start=0&latitude=38.880918&longitude=121.546376&sign=e306b4ad817d9c2df47cd4097ac69244"

#import "OPCountryController.h"
#import "SceneryDetailController.h"
#import "CountrySceneryModel.h"
#import "SceneryCell.h"

#import "OPRequesterManager.h"
#import <UIImageView+WebCache.h>
@interface OPCountryController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UIImageView *headView;
@property (nonatomic, strong) UILabel *countryNameLabel;
@property (nonatomic, strong) UILabel *goAndLikeLabel;
@property (nonatomic, strong) UITableView *mainTableView;

@property (nonatomic, strong) NSMutableArray *scenerySourceArray;
@property (nonatomic, strong) CountrySceneryModel *sceneryModel;

@end

@implementation OPCountryController


-(NSMutableArray *)scenerySourceArray
{
    if (_scenerySourceArray == nil) {
        _scenerySourceArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _scenerySourceArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleLable.text = self.model.name;
    [self.leftButton setImage:[UIImage imageNamed:@"meishi-back"] forState:(UIControlStateNormal)];
    [self.leftButton addTarget:self action:@selector(back) forControlEvents:(UIControlEventTouchUpInside)];
    
    _headView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 160)];
    [_headView sd_setImageWithURL:[NSURL URLWithString:self.model.cover_route_map_cover]];
    _countryNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 90, 60, 30)];
    _countryNameLabel.text = self.model.name;
    _countryNameLabel.textColor = [UIColor whiteColor];
    _countryNameLabel.font = [UIFont systemFontOfSize:15];
    _goAndLikeLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 120, SCREEN_WIDTH-60, 20)];
    NSString *visitStr = [NSString stringWithFormat:@"%ld",self.model.visited_count];
    NSString *wishStr = [NSString stringWithFormat:@"%ld",self.model.wish_to_go_count];
    _goAndLikeLabel.text = [NSString stringWithFormat:@"%@人 去过   %@人 喜欢",visitStr,wishStr];
    _goAndLikeLabel.textColor = [UIColor whiteColor];
    _goAndLikeLabel.font = [UIFont systemFontOfSize:15];
    [_headView addSubview:_countryNameLabel];
    [_headView addSubview:_goAndLikeLabel];
    
    
    _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-114)];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    _mainTableView.tableHeaderView = _headView;
    _mainTableView.bounces = NO;
    [self.view addSubview:_mainTableView];
    
    
    [self.mainTableView registerClass:[SceneryCell class] forCellReuseIdentifier:@"scenery"];
    
    [self requestData];
    
    
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)requestData
{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    [OPRequesterManager requestWithType:GET urlString:[NSString stringWithFormat:@"%@%@%@",FIRST,self.model.countryId,LAST] parDic:parDic finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        for (NSDictionary *placeDic in dic[@"items"]) {
            _sceneryModel = [[CountrySceneryModel alloc]init];
            [_sceneryModel setValuesForKeysWithDictionary:placeDic];
            [self.scenerySourceArray addObject:_sceneryModel];
        }
        
        [self.mainTableView reloadData];
        
        
    } error:^(NSError *error) {
        NSLog(@"--------------国家详情界面数据请求失败---------------");
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _scenerySourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SceneryCell *cell = [ tableView dequeueReusableCellWithIdentifier:@"scenery" forIndexPath:indexPath];
    [cell setConfigureModel:_scenerySourceArray[indexPath.row]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SceneryDetailController *SDC = [[SceneryDetailController alloc]init];
    SDC.detailId = [_scenerySourceArray[indexPath.row] detailId];
    [self.navigationController pushViewController:SDC animated:YES];
 
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 220;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
